//
//  VoteMasterService.m
//  homeland
//
//  Created by gzbugu on 14-7-28.
//  Copyright (c) 2014年 xzysun. All rights reserved.
//  民意征集/我要投票服务类
//

#import "VoteMasterService.h"
#import "UIImageView+WebCache.h"
#import "VoteMasterEntity.h"
#import "DataStoreService.h"
#import "VoteMasterCell.h"
#import "UserInfoService.h"

@implementation VoteMasterService
//请求参数集
NSMutableDictionary *params;

- (void)loadDataWithKey:(NSString *)keyOrNil{
    _currentPageNo = 1;
    if (self.dataList) {
        self.dataList = nil;
    }
    //民意征集/我要投票
    _dataItemName = @"VoteMasterEntity";
    NSDictionary *_config = [super paramDic];
    
    params = [NSMutableDictionary dictionary];
    UserInfoService *userInfo=[UserInfoService getUserInfoService];
    [params setValue:userInfo.userInfo.Id forKey:@"userID"];
    [params setValue:[_config objectForKey:@"communityId"] forKey:@"communityId"];
    [params setValue:[_config objectForKey:@"partyMember"] forKey:@"partyMember"];
    //communityCategory:1表示民意征集，2表示我要投票
    [params setValue:[_config objectForKey:@"communityCategory"] forKey:@"communityCategory"];
    
    if (keyOrNil) {
        [params setValue:keyOrNil forKey:@"voteTitle"];
    } else {
        [params setValue:@"" forKey:@"voteTitle"];
    }
    [self doHttpPost:SERVER_URL_VOTE_MASTER_LIST headers:nil path:[NSString stringWithFormat:@"/%d",_currentPageNo] parameters:params];
}

- (void)loadMoreDataWithKey:(NSString *)keyOrNil{
    if (!params || [params count]==0) {
        params = [NSMutableDictionary dictionary];
    }
    if (keyOrNil) {
        [params setValue:keyOrNil forKey:@"voteTitle"];
    } else {
        [params setValue:@"" forKey:@"voteTitle"];
    }
    //民意征集/我要投票
    _dataItemName = @"VoteMasterEntity";
    [self doHttpPost:SERVER_URL_VOTE_MASTER_LIST headers:nil path:[NSString stringWithFormat:@"/%d",_currentPageNo + 1] parameters:params];
}

- (CGFloat)heightForCellAtRow:(NSIndexPath *)index{
    return 90.0f;//民意征集/我要投票每行105
}

/**展示数据*/
- (void)configCell:(UITableViewCell *)cell atIndex:(NSIndexPath *)index{
    if (self.dataList == nil || self.dataList.count == 0) {
        return;
    }
    //民意征集/我要投票
    VoteMasterEntity *item = [self.dataList objectAtIndex:index.row];
    VoteMasterCell *tempCell = (VoteMasterCell *)cell;
    if (index.row<3) {
        tempCell.titleLabel.text = [CommonUtil dealNullObject:item.voteTitle];
        tempCell.titleLabel.textColor = [UIColor redColor];
    }else{
       tempCell.titleLabel.text = [CommonUtil dealNullObject:item.voteTitle];
    }
    
    tempCell.detailInfoView.text = [CommonUtil dealNullObject:item.voteContext];
    if (item.countNum && [item.countNum intValue]>0) {//是否已投票
        tempCell.hasVote.hidden = NO;
    }else{
        if(item.isEnd && [item.isEnd intValue]==0){//已结束
            tempCell.hasVote.text = @"已结束";
            tempCell.hasVote.textColor = [UIColor darkGrayColor];
            tempCell.hasVote.hidden = NO;
        }else{
            tempCell.hasVote.hidden = YES;
        }
    }
    tempCell.timeLabel.text = [CommonUtil formatDateStr:item.publishDate style:@"yyyy-MM-dd"];
    //图片
    NSString *pic = [NSString stringWithFormat:@"http://%@%@", SERVER_URL_COMMUNITY_VOTE_IMAGE, item.voteImage];
    
    __block UIActivityIndicatorView *activityIndicator;
    __weak UIImageView *weakImageView = tempCell.imageView;
    [tempCell.imageView setImageWithURL:[NSURL URLWithString:pic] placeholderImage:[UIImage imageNamed:@"placeholder.png"] options:SDWebImageProgressiveDownload|SDWebImageRetryFailed|SDWebImageLowPriority progress:^(NSUInteger receivedSize, long long expectedSize){
         if (!activityIndicator){
             [weakImageView addSubview:activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]];
             activityIndicator.center = weakImageView.center;
             [activityIndicator startAnimating];
         }
     } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType){
         [activityIndicator removeFromSuperview];
         activityIndicator = nil;
     }];
}

@end
